package com.qf.day05;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

/**
 * 如何使用JDK中已经存在的类
 */
public class Demo06 {
    public static void main(String[] args) throws IOException {

//        Scanner scanner = new Scanner(System.in);

//        Scanner scanner = new Scanner("hello world hello java");
//
//        while (scanner.hasNext()) {
//            System.out.println(scanner.next());
//        }

//        String filepath = "D:\\00qd2306\\name2306.txt";
//        // 创建文件对象
//        File file = new File(filepath);
//        // 基于文件构造了一个扫描器对象
//        Scanner scanner = new Scanner(file);
//        while (scanner.hasNextLine()) {
//            System.out.println(scanner.nextLine());
//        }

        //扫描器读取网页的内容

        // 创建url对象
        URL url = new URL("http://www.baidu.com");

        // 创建连接对象
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 设置 http 请求的方式  get，post
        conn.setRequestMethod("GET");

        // 获取 响应状态码
        int responseCode = conn.getResponseCode();
        System.out.println(responseCode);

        // 把连接对象中的InputStream对象获取到
        InputStream inputStream = conn.getInputStream();
        Scanner scanner = new Scanner(inputStream);

        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }

    }
}
